/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Golf_Manager.editor;

import com.toedter.calendar.JDateChooser;
import java.awt.Component;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

/**
 *
 * @author Administrator
 */
public class CellJDateChooserEditor extends AbstractCellEditor implements TableCellEditor {
    JDateChooser jDateChooser;
    Object valorActual;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    
    public CellJDateChooserEditor() {
    }

    @Override
    public boolean stopCellEditing() {
        //System.out.println("stopCellEditing");
        valorActual = jDateChooser.getDate() != null ? sdf.format(jDateChooser.getDate()) : null;
        //System.out.println("valorActual :" + valorActual);
        return super.stopCellEditing(); //To change body of generated methods, choose Tools | Templates.
    }
    
    @Override
    public Object getCellEditorValue() {
        //System.out.println("getCellEditorValue");
        //System.out.println("valorActual : " + valorActual);
        return valorActual;
    }

    public void setValorActual(Object valorActual) {
        //System.out.println("setValorActual");
        //System.out.println("valorActual : " + valorActual);
        this.valorActual = valorActual;
    }
    
    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        this.jDateChooser = new com.toedter.calendar.JDateChooser();
        this.jDateChooser.setDateFormatString("yyyy-MM-dd");
        //this.jDateChooser.getDateEditor().setEnabled(false);
        //System.out.println("getTableCellEditorComponent : " + value);
        this.jDateChooser.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                //System.out.println("propertyChange");
                
                if(evt.getPropertyName().equals("date")){
                    
                    //System.out.println(evt);
                    //com.toedter.calendar.JDateChooser eventObj = (com.toedter.calendar.JDateChooser)evt.getSource();
                    //valorActual = eventObj.getDate() != null ? sdf.format(eventObj.getDate()) : null;
                    
                }
                stopCellEditing();
            }
        }); 
        
        this.jDateChooser.addFocusListener(new FocusAdapter() {

            @Override
            public void focusGained(FocusEvent e) {
                super.focusGained(e); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void focusLost(FocusEvent e) {
                super.focusLost(e); //To change body of generated methods, choose Tools | Templates.;
                stopCellEditing();
            }
            
        });
        
        if(value != null){
            try {
                this.jDateChooser.setDate(sdf.parse(String.valueOf(value)));
            } catch (ParseException ex) {
                Logger.getLogger(CellJDateChooserEditor.class.getName()).log(Level.SEVERE, null, ex);
            }
        } 
        
        return this.jDateChooser;
    }
 
}
